﻿using MediatR;
using Production_Get.API.DTO;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.DemandPlanning;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.MaterialManagement.MaterialRequirementsPlanning
{
    public class MaterialPlanningSeachHandler : IRequestHandler<MaterialPlanningSeachCommon, Result>
    {
        private readonly IBaseRepositoryGet<MaterialPlanning> MaterialPlanningRepository;

        public MaterialPlanningSeachHandler(IBaseRepositoryGet<MaterialPlanning> materialPlanningRepository)
        {
            MaterialPlanningRepository = materialPlanningRepository;
        }
        public async Task<Result> Handle(MaterialPlanningSeachCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            string sql = "SELECT * FROM MaterialPlanning where IsDelete=0";
            var list = MaterialPlanningRepository.GetAll(sql).ToList();
            if (!string.IsNullOrEmpty(request.PlanningCode))
            {
                list = list.Where(x => x.PlanningCode.Contains(request.PlanningCode)).ToList();
            }

            if (!string.IsNullOrEmpty(request.PlanningName))
            {
                list = list.Where(x => x.PlanningName.Contains(request.PlanningName)).ToList();
            }
            if (list != null)
            {
                result.Code = (int)CommonErrorCodes.Success;
                result.Data = list;
                result.Msg = "显示成功";
            }
            else
            {
                result.Msg = "显示失败";
            }
            return result;
        }
    }
}
